package cn.hx.mall_goods.controller;


import cn.hx.mall_goods.entity.Coupon;
import cn.hx.mall_goods.entity.CouponProductCategoryRelation;
import cn.hx.mall_goods.service.CouponProductCategoryRelationService;
import cn.hx.mall_goods.service.CouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Controller
public class CouPonController {

    @Resource
    private CouponService couponService;
    @Resource
    private CouponProductCategoryRelationService couponProductCategoryRelationService;

//    显示全部优惠券
    @GetMapping("selectAllCoupon")
    public ModelAndView getSelectAllCoupon(){

        Map<String,String> map = new HashMap<>();

        List<CouponProductCategoryRelation> relations = couponProductCategoryRelationService.list(null);

        map.put("0","全部优惠券");
        for (CouponProductCategoryRelation relation : relations) {
            map.put(relation.getProductCategoryId().toString(),relation.getProductCategoryName());
        }

        System.out.println(map);

        ModelAndView modelAndView = new ModelAndView("couponAll");
        modelAndView.addObject("couponList",couponService.list(null));
        modelAndView.addObject("classify",map);
        return modelAndView;
    }

    @GetMapping("selectCoupon")
    @ResponseBody
    public List<Coupon> getCoupon(Integer id){

        List<Coupon> id1=null;
        if (id==0){
           id1 = couponService.list(null);
           return id1;
        }

        List<CouponProductCategoryRelation> categoryId = couponProductCategoryRelationService.list(new QueryWrapper<CouponProductCategoryRelation>().eq("product_category_id", id));

        List<Long> collect = categoryId.stream().map(CouponProductCategoryRelation::getCouponId).collect(Collectors.toList());

         id1 = couponService.list(new QueryWrapper<Coupon>().in("id", collect));

        return id1;
    }

}
